//提问问题的id
var persondata;
var imgId = $("#img").val();
var queId = location.search.split("=")[1];

var getUid = $("#uid").val();
var getName = $("#nickName").val();
console.log("头像id-->"+imgId);
console.log("问题id-->"+queId);

/*点赞控制*/
$(document).on("click",".iconstyle1",function(){
    var num = $(this).parent().next().text();
    var color = $(this).css("color");
    //alert(color);
    var numadd = Number(num)+1;
    var answerId = $(this).parent().parent().parent().parent().attr("ans-id");
    
    //alert(answerId);
    /*还没有点赞，可以点赞*/
    if( color == "rgb(204, 204, 204)"){
        
        $(this).css({"color":"red"});
        $(this).parent().next().text(numadd);
        
        $.ajax({
            type:"post",
            url:"discuss_agree",
            dataType:"json",
            data:{
                uid:getUid,  //当前登录用户ID
                ansId:answerId,
                f:"true"
            }
        })
    }
    else if(color == "rgb(255, 0, 0)"){
        /*已经点赞，取消点赞*/    
        $(this).css({"color":"#ccc"});
        $(this).parent().next().text(--num);
        //alert(answerId);
        $.ajax({
            type:"post",
            url:"discuss_agree",
            dataType:"json",
            data:{
                uid:getUid,  //当前登录用户ID
                ansId:answerId,
                f:"false"
            }
        })
    }
    
});

/*高度控制，评论很少时控制*/
function watchHeight(){
     var height = $(".comments-list").height();
    //alert(height);
    if(height<700){
        $(".comments-list").height("900px");
    }
}


/*从cookie里面获取的用户信息*/
var loginperson = {
    "loginUserId" : getUid,
    "loginUserName" : getName,
    "loginUserImg" : imgId
};



var time = new Date();
var curtime = time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate();
console.log(curtime);

/*回复问题1*/
$("#comment1").click(function(){
    var comment = $(this).prev().val();
    
    var $appendcomment1 = "<div class='comments_box'><div class='comments-ansbox1'>"+
                        "<div class='ansbox1-perimg'>"+
                            "<img src='images/usericon/user"+loginperson.loginUserImg+".jpg' >"+
                        "</div>"+
                        "<div class='ansbox1-pername'>"+loginperson.loginUserName+"</div>"+
                        "<div class='ansbox1-content'>"+
                           "<span>"+comment+"</span>"+
                           "<div class='ansbox1-time'>"+
                                "<span class='ansbox1-quetime'>"+curtime+"</span>"+
                                "<span class='ansbox1-icon'>"+
                                    "<svg class='icon iconstyle' aria-hidden='true'>"+
                                      "<use xlink:href='#icon-writefill'></use>"+
                                    "</svg>"+
                                "</span>"+
                                "<span class='comment-rep'>回复</span>"+
                                "<span>"+ 
                                    "<svg class='icon iconstyle iconstyle1' aria-hidden='true'>"+
                                      "<use xlink:href='#icon-appreciate_fill_light'></use>"+
                                    "</svg>"+
                                "</span>"+
                                "<span>0</span>"+
                            "</div>"+
                        "</div>"+
                    "</div><div class='comments_ans'></div></div>";
    
    if(comment == "" || comment == null){
        alert("请填写评论");
    }else{
        //发送一级评论
        $.ajax({
             type:"post",
             contentType: "application/x-www-form-urlencoded; charset=utf-8",
             url :"http://192.168.30.215:8080/E-learning/discuss_addComment",
             dataType : "json",
             data:{queId:persondata.queId,comments:comment,uid:getUid},   //这里也要传入当前登录用户ID 
             success:function(data){
                 //alert(data);
                 if(data){
                      $(".comments-answrite1").before($appendcomment1);
                 }
             },
             error:function(){
                 alert("发送数据失败");
             }

        })
        
    }
    
    
    
    watchHeight();
})


/*回复问题2*/
$(document).on("click",".commit2",function(){
    //alert($(this).parent().find("textarea").val());
    var this1 = $(this);
    var comment = $(this).parent().find("textarea").val();
    var content = comment.substring(comment.indexOf("：")+1,comment.length);
    
    if(comment.indexOf("回复")>-1){
        var name = comment.split(" ")[1];
        if(content == "" || content == null){
            alert("请填写评论！");
        }else{
            //alert(content);
            var $appendHtml = "<div class='comments-ansbox2'>"+
                                    "<div class='ansbox2-perimg'>"+
                                        "<img src='images/usericon/user"+loginperson.loginUserImg+".jpg'>"+
                                    "</div>"+
                                    "<div class='ansbox2-pername'>"+
                                        "<span class='repUserName'>"+loginperson.loginUserName+"</span>"+
                                        "<span>回复</span>"+
                                        "<span>"+name+"</span>"+
                                    "</div>"+
                                    "<div class='ansbox2-content'>"+
                                       "<span>"+content+"</span>"+
                                        "<div class='ansbox2-time'>"+
                                            "<span class='ansbox2-quetime'>"+curtime+"</span>"+
                                            "<span class='ansbox2-icon'>"+
                                                "<svg class='icon iconstyle' aria-hidden='true'>"+
                                                  "<use xlink:href='#icon-post'></use>"+
                                                "</svg>"+
                                            "</span>"+
                                            "<span class='answer-rep'>回复</span>"+
                                        "</div>"+
                                    "</div>"+
                                "</div>";
            
            /*==================在这里获取回复的是谁===================*/
            
            var repUserId = $(this).parent().parent().prev().find(".repUserName").attr("answer2-id");
            var firstComment = $(this).parent().parent().parent().prev().attr("ans-id");
            alert("回复谁"+repUserId+"一级评论id--"+firstComment);
            
            //请求交互
                $.ajax({
                    type:"post",
                    url:"discuss_save",
                    data:{
                        uid:getUid,
                        ansId :firstComment,
                        repContent:content,
                        repToId: repUserId
                    },
                    datatype:"json",
                    success:function(data){
                        //console.log(data);
                        if(data){
                            this1.parent().parent().before($appendHtml);
                            this1.parent().parent().remove();
                        }else{
                            console.log("二级回复失败");
                        }
                    },
                    error:function(){
                        console.log("二级回复失败");
                    }
                })
            
           
            
            
            watchHeight();
        }
    }
    else{
        //这里回复的是一级评论
        if(comment == null || comment == ""){
            alert("请填写评论");
        }
        else{
             var $appendHtml = "<div class='comments-ansbox2'>"+
                                    "<div class='ansbox2-perimg'>"+
                                        "<img src='images/usericon/user"+loginperson.loginUserImg+".jpg'>"+
                                    "</div>"+
                                    "<div class='ansbox2-pername'>"+
                                        "<span class='repUserName'>"+loginperson.loginUserName+"</span>"+
                                    "</div>"+
                                    "<div class='ansbox2-content'>"+
                                       "<span>"+comment+"</span>"+
                                        "<div class='ansbox2-time'>"+
                                            "<span class='ansbox2-quetime'>"+curtime+"</span>"+
                                            "<span class='ansbox2-icon'>"+
                                                "<svg class='icon iconstyle' aria-hidden='true'>"+
                                                  "<use xlink:href='#icon-post'></use>"+
                                                "</svg>"+
                                            "</span>"+
                                            "<span class='answer-rep'>回复</span>"+
                                        "</div>"+
                                    "</div>"+
                                "</div>";
            
            
            //如果一个评论都没有
            var $appendHtmans = "<div class='comments_ans'><div class='comments-ansbox2'>"+
                                    "<div class='ansbox2-perimg'>"+
                                        "<img src='images/usericon/user"+loginperson.loginUserImg+".jpg'>"+
                                    "</div>"+
                                    "<div class='ansbox2-pername'>"+
                                        "<span class='repUserName'>"+loginperson.loginUserName+"</span>"+
                                    "</div>"+
                                    "<div class='ansbox2-content'>"+
                                       "<span>"+comment+"</span>"+
                                        "<div class='ansbox2-time'>"+
                                            "<span class='ansbox2-quetime'>"+curtime+"</span>"+
                                            "<span class='ansbox2-icon'>"+
                                                "<svg class='icon iconstyle' aria-hidden='true'>"+
                                                  "<use xlink:href='#icon-post'></use>"+
                                                "</svg>"+
                                            "</span>"+
                                            "<span class='answer-rep'>回复</span>"+
                                        "</div>"+
                                    "</div>"+
                                "</div></div>";
            
            
            /*====================在这里获取回复的是谁===================*/
            
            var ansbox = $(this).parent().parent().prev().attr("class");
            //alert(ansbox);
            if(ansbox == "comments-ansbox2"){
                //找到是回复的谁的id
                var repUserId1 = $(this).parent().parent().parent().prev().find(".ansbox1-pername").attr("answer1-id");
                //找到一级评论的id
                var firstComment = $(this).parent().parent().parent().prev().attr("ans-id");
                var json = {
                    uid:getUid,
                    ansId :firstComment,
                    repContent:content,
                    repToId: repUserId1
                };
                
                
                alert("回复谁—"+repUserId1+"问题id——"+firstComment);
                
                //请求交互
                $.ajax({
                    type:"post",
                    url:"discuss_save",
                    data:{
                        uid:getUid,
                        ansId :firstComment,
                        repContent:content,
                        repToId: repUserId1
                    },
                    datatype:"json",
                    success:function(data){
                        console.log(data);
                        if(data){
                            this1.parent().parent().parent().append($appendHtml);
                            this1.parent().parent().remove();
                        }else{
                            console.log("一级回复失败");
                        }
                    },
                    error:function(){
                        console.log("一级回复失败");
                    }
                })
                
                
            }
            else{
                var repUserId2 = $(this).parent().parent().prev().find(".ansbox1-pername").attr("answer1-id");
                var firstComment = $(this).parent().parent().prev().attr("ans-id");
                alert("回复谁—"+repUserId2+"问题id——"+firstComment);

                
                //请求交互
                $.ajax({
                    type:"post",
                    url:"discuss_save",
                    data:{
                        uid:getUid,
                        ansId :firstComment,
                        repContent:content,
                        repToId: repUserId2
                    },
                    datatype:"json",
                    success:function(data){
                        console.log(data);
                        if(data){
                            this1.parent().parent().prev().after($appendHtmans);
                            this1.parent().parent().remove();
                        }else{
                            console.log("一级回复失败");
                        }
                    },
                    error:function(){
                        console.log("一级回复失败");
                    }
                })
                
                 
            }
        
            
            watchHeight();
        }
    }
    
    
})



var $comment2 = "<div class='comments-answrite2'>"+
                   "<img src='images/usericon/user"+loginperson.loginUserImg+".jpg'>"+    //登录用户的数据
                    "<form>"+
                        "<textarea id='' cols='70' rows='6'></textarea>"+
                        "<button class='cancel2' type='button'>取消</button>"+
                        "<button class='commit2' type='button'>评论</button>"+
                    "</form>"+
                "</div>";


/*=================回复一级评论框=======================*/
$(document).on("click",".comment-rep",function(){
    var this1 = $(this).parent().parent().parent();
    var parent = this1.parent();
    var comments = parent.find(".comments-ansbox2").length;
   // alert(comments);
    var answerbox;
    //做一个判断，判断有没有回复评论框
    if(comments>0){
         answerbox= parent.find(".comments-ansbox2").eq(comments-1).next().length;
        /*判断最后是不是有评论框*/
        if(answerbox == 0 ){
            if(parent.find(".comments_ans").length>0){
                parent.find(".comments_ans").append($comment2); 
            }else{
                parent.append($comment2);
            }
        }else{
            /*如果有评论框，就将里面的内情哦那个去表*/
            parent.find(".comments-answrite2").last().find("textarea").val("");
        }
    }else{
        var answritelen =  parent.find(".comments-answrite2").length;
        if(answritelen == 0){
            
            parent.find(".comments-ansbox1").parent().append($comment2); 
        }
    }
    
    
    
});


/*回复二级评论框*/
$(document).on("click",".answer-rep",function(){
    
    var this1 = $(this).parent().parent().parent();
    var parent = this1.parent();
    var nextbox = this1.next();
    
    /*找是回复的谁*/
    var name = this1.find(".repUserName").text();
    
    //判断自己下面有咩有回复框，有就不添加
    if(nextbox.attr("class") != "comments-answrite2"){
        this1.after("<div class='comments-answrite2'><img src='images/lunbo/4.jpg' ><form><textarea id='' cols='70' rows='6'>回复 "+name+" ：</textarea><button class='cancel2' type='button'>取消</button><button class='commit2' type='button'>评论</button></form></div>");
    }else{
        parent.find(".comments-answrite2").last().find("textarea").val("回复 "+name+" ：");
    }
});


/*------------点击二级评论框的取消按钮-----------------------*/
$(document).on("click",".cancel2",function(){
    $(this).parent().parent().remove();
});


/*---------------------------------------初始化页面，第一次，刷新页面----------------*/
   $.ajax({
         type:"get",
         url :"discuss_detail",
         dataType : "json",
         data:{
        	 uid:getUid,
        	 queId:queId
        	 },    //这里也要传入登录者的ID
         success:function(data){
             
             persondata = {
                 "queId":data.queId,    //问题Id
                 "quesUserId":data.quesUserId  //提问者ID--》获取提问者的姓名
             }
             

        $("#comments-img").attr("src","images/usericon/user"+data.quesUserImg+".jpg");
        $("#perimg-name").text(data.quesUserName);
        $("#comments-quename").text(data.queContent);
        $("#comments-quetime").text(data.queTime);
        $("#class-name").text(data.videoName);
        
        //判断是否有评论
        if(data.comments){
            $("#comments-count").text(data.comments.length);
        }


        var $appendHtml = "";
        var comment = data.comments;
        //这里是遍历了
        $.each(comment,function(index,value){
            
            $appendHtml += "<div class='comments_box'>";
            if(value.ifAgree == "0"){
           // console.log(value.ifAgree+"1111");
                /*----------------没有赞过的标志-----------------------*/
                 $appendHtml += "<div class='comments-ansbox1' ans-id='"+value.ansNumber+"'>"+
                                "<div class='ansbox1-perimg'>"+
                                    "<img src='images/usericon/user"+value.ansUserImg+".jpg' >"+
                                "</div>"+
                                "<div class='ansbox1-pername' answer1-id='"+value.ansUserId+"'>"+value.ansUserName+"</div>"+
                                "<div class='ansbox1-content'>"+
                                   "<span>"+value.ansContent+"</span>"+
                                   "<div class='ansbox1-time'>"+
                                        "<span class='ansbox1-quetime'>"+value.ansTime+"</span>"+
                                        "<span class='ansbox1-icon'>"+
                                            "<svg class='icon iconstyle' aria-hidden='true'>"+
                                              "<use xlink:href='#icon-writefill'></use>"+
                                            "</svg>"+
                                        "</span>"+
                                        "<span class='comment-rep'>回复</span>"+
                                        "<span>"+ 
                                            "<svg class='icon iconstyle iconstyle1' aria-hidden='true'>"+
                                              "<use xlink:href='#icon-appreciate_fill_light'></use>"+
                                            "</svg>"+
                                        "</span>"+
                                        "<span>"+value.agreeNumber+"</span>"+
                                    "</div>"+
                                "</div>"+
                            "</div>";
            }else if(value.ifAgree == "1"){
               // console.log(value.ifAgree+"2222");
                /*-------------------已经赞过的标志---------------------*/
                 $appendHtml += "<div class='comments-ansbox1' ans-id='"+value.ansNumber+"'>"+
                                "<div class='ansbox1-perimg'>"+
                                    "<img src='images/usericon/user"+value.ansUserImg+".jpg' >"+
                                "</div>"+
                                "<div class='ansbox1-pername' answer1-id='"+value.ansUserId+"'>"+value.ansUserName+"</div>"+
                                "<div class='ansbox1-content'>"+
                                   "<span>"+value.ansContent+"</span>"+
                                   "<div class='ansbox1-time'>"+
                                        "<span class='ansbox1-quetime'>"+value.ansTime+"</span>"+
                                        "<span class='ansbox1-icon'>"+
                                            "<svg class='icon iconstyle' aria-hidden='true'>"+
                                              "<use xlink:href='#icon-writefill'></use>"+
                                            "</svg>"+
                                        "</span>"+
                                        "<span class='comment-rep'>回复</span>"+
                                        "<span >"+ 
                                            "<svg class='icon iconstyle iconstyle1 has_click' aria-hidden='true'>"+
                                              "<use xlink:href='#icon-appreciate_fill_light'></use>"+
                                            "</svg>"+
                                        "</span>"+
                                        "<span>"+value.agreeNumber+"</span>"+
                                    "</div>"+
                                "</div>"+
                            "</div>";
            }
           


           

            //一级评论完成之后，这里是判断二级评论，然后在二级里面查看是里面是不是回复的别人
            //先判断是否有回复
            if(value.replay){
                if(value.replay.length>0){
                    $appendHtml += "<div class='comments_ans'>";
                    $.each(value.replay,function(ind,val){
                        if(val.replyTo == null || val.replyTo == ""){
                            /*-----------------不是回复的别人-------------------*/
                            $appendHtml += "<div class='comments-ansbox2'>"+
                                                "<div class='ansbox2-perimg'>"+
                                                    "<img src='images/usericon/user"+val.repUserImg+".jpg'>"+
                                                "</div>"+
                                                "<div class='ansbox2-pername'>"+
                                                    "<span class='repUserName' answer2-id='"+val.repUserId+"'>"+val.repUserName+"</span>"+
                                                "</div>"+
                                                "<div class='ansbox2-content'>"+
                                                   "<span>"+val.repContent+"</span>"+
                                                    "<div class='ansbox2-time'>"+
                                                        "<span class='ansbox2-quetime'>"+val.repTime+"</span>"+
                                                        "<span class='ansbox2-icon'>"+
                                                            "<svg class='icon iconstyle' aria-hidden='true'>"+
                                                              "<use xlink:href='#icon-post'></use>"+
                                                            "</svg>"+
                                                        "</span>"+
                                                        "<span class='answer-rep'>回复</span>"+
                                                    "</div>"+
                                                "</div>"+
                                            "</div>";
                        }else{
                            /*------------------------是回复的别人--------------------------*/
                            $appendHtml += "<div class='comments-ansbox2'>"+
                                                "<div class='ansbox2-perimg'>"+
                                                    "<img src='images/usericon/user"+val.repUserImg+".jpg'>"+
                                                "</div>"+
                                                "<div class='ansbox2-pername'>"+
                                                    "<span class='repUserName'  answer2-id='"+val.repUserId+"' >"+val.repUserName+"</span>"+
                                                    "<span>回复</span>"+
                                                    "<span >"+val.replyToName+"</span>"+
                                                "</div>"+
                                                "<div class='ansbox2-content'>"+
                                                   "<span>"+val.repContent+"</span>"+
                                                    "<div class='ansbox2-time'>"+
                                                        "<span class='ansbox2-quetime'>"+val.repTime+"</span>"+
                                                        "<span class='ansbox2-icon'>"+
                                                            "<svg class='icon iconstyle' aria-hidden='true'>"+
                                                              "<use xlink:href='#icon-post'></use>"+
                                                            "</svg>"+
                                                        "</span>"+
                                                        "<span class='answer-rep'>回复</span>"+
                                                    "</div>"+
                                                "</div>"+
                                            "</div>";
                        }

                    });

                $appendHtml += "</div>";
                }
            }
            
            //每一次循环要包裹
            $appendHtml += "</div>";

        })
        
        //$("#comments-list").append($appendHtml);
        $(".comments-content").after($appendHtml);

    },
         error:function(){
             console.log("请求数据出错");
         }

    })